package com.ruoyi.salaryBase.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import com.ruoyi.salaryBase.domain.BaseSalaryChangeVO;
import com.ruoyi.salaryBase.domain.BaseSalaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.salaryBase.mapper.BaseSalaryMapper;
import com.ruoyi.salaryBase.domain.BaseSalary;
import com.ruoyi.salaryBase.service.IBaseSalaryService;

/**
 * 员工基本薪资Service业务层处理
 * 
 * @author cxs
 * @date 2025-05-30
 */
@Service
public class BaseSalaryServiceImpl implements IBaseSalaryService 
{
    @Autowired
    private BaseSalaryMapper baseSalaryMapper;

    /**
     * 查询员工基本薪资
     * 
     * @param id 员工基本薪资主键
     * @return 员工基本薪资
     */
    @Override
    public BaseSalary selectBaseSalaryById(Long id)
    {
        return baseSalaryMapper.selectBaseSalaryById(id);
    }

    /**
     * 查询员工基本薪资列表
     * 
     * @param baseSalary 员工基本薪资
     * @return 员工基本薪资
     */
    @Override
    public List<BaseSalaryVO> selectBaseSalaryList(BaseSalaryVO baseSalary)
    {
        /*return baseSalaryMapper.selectBaseSalaryList(baseSalary);*/
        return baseSalaryMapper.selectList(baseSalary);
    }

    /**
     * 新增员工基本薪资
     * 
     * @param baseSalary 员工基本薪资
     * @return 结果
     */
    @Override
    public int insertBaseSalary(BaseSalary baseSalary)
    {
        return baseSalaryMapper.insertBaseSalary(baseSalary);
    }

    /**
     * 修改员工基本薪资
     * 
     * @param baseSalary 员工基本薪资
     * @return 结果
     */
    @Override
    public int updateBaseSalary(BaseSalary baseSalary)
    {
        //先获取要查的数据
        BaseSalary old = baseSalaryMapper.selectBaseSalaryById(baseSalary.getId());

        //如果薪资变化
        if (old.getTotalSalary().compareTo(baseSalary.getTotalSalary()) != 0){
            //把旧数据插入数据库
            old.setIsCurrent(0);
            old.setId(null);
            baseSalaryMapper.insertBaseSalary(old);
        }

        //修改
        baseSalary.setCreatedAt(new Date());
        int i = baseSalaryMapper.updateBaseSalary(baseSalary);


        return i;
    }

    /**
     * 批量删除员工基本薪资
     * 
     * @param ids 需要删除的员工基本薪资主键
     * @return 结果
     */
    @Override
    public int deleteBaseSalaryByIds(Long[] ids)
    {
        return baseSalaryMapper.deleteBaseSalaryByIds(ids);
    }

    /**
     * 删除员工基本薪资信息
     * 
     * @param id 员工基本薪资主键
     * @return 结果
     */
    @Override
    public int deleteBaseSalaryById(Long id)
    {
        return baseSalaryMapper.deleteBaseSalaryById(id);
    }

    /**
     * 查看调薪明细表
     * @param baseSalary
     * @return
     */
    @Override
    public List<BaseSalaryChangeVO> selectChangeList(BaseSalaryVO baseSalary) {
        return baseSalaryMapper.selectChangeList(baseSalary);
    }

    /**
     * 统计
     * @return
     */
    @Override
    public List<Map<String, Object>> statistics() {
        return baseSalaryMapper.statistics();
    }

    /**
     * 部门统计
     * @param baseSalary
     */
    @Override
    public HashMap deptStatistics(BaseSalaryVO baseSalary) {
        List<Map<String, Object>> deptList = baseSalaryMapper.deptStatistics(baseSalary);

        ArrayList<String> categories  = new ArrayList<>();
        ArrayList<Double> values = new ArrayList<>();

        for (Map<String, Object> map : deptList) {
            categories.add(map.get("categories").toString());
            values.add(Double.parseDouble(map.get("salary").toString()));
        }

        HashMap<String, Object> result = new HashMap<>();
        result.put("categories", categories);
        result.put("values", values);

        return result;

    }
}
